package com.lmind.core.publish;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class SimplePublisherTestCase {
	
	@BeforeClass
	public static void beforeClass() throws Exception {
		
	}

	@Test
	public void test() throws Exception {
		
		String news = "New York Times";
		
		SimplePublisher<Object> pub = new SimplePublisher<Object>();
		
		MySubscriber sub = new MySubscriber();
		
		
		pub.subscribe(sub, news);
		pub.publish(news, "The World War");
		Assert.assertTrue(sub.lastName.equals(news));
		Assert.assertTrue(sub.lastPublication.equals("The World War"));
		
		try {
			pub.subscribe(sub, news);
			Assert.assertTrue(false);
		} catch (SubscribeException e) {
			Assert.assertTrue(true);
		}
		
		pub.unsubscribe(sub, news);
		sub.lastPublication = null;
		pub.publish(news, "The World War");
		Assert.assertTrue(sub.lastPublication == null);
		
		try {
			pub.unsubscribe(sub, news);
			Assert.assertTrue(false);
		} catch (SubscribeException e) {
			Assert.assertTrue(true);
		}
	}
	
	
	public static class MySubscriber implements Subscriber<Object> {
		
		public String lastName;
		
		public Object lastPublication;
		
		@Override
		public void feed(String name, Object publication) {
			lastName = name;
			this.lastPublication = publication;
		}
	}
	
	

}
